/**
 * 评价管理
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>评价管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item>
        <el-button size="small" width="120" type="primary" icon="el-icon-upload el-icon--right" @click="handleEdit()">
          表格导入</el-button>
        <el-button size="small" width="120" type="primary" icon="el-icon-upload el-icon--right" @click="handleEdit()">
          上传文件</el-button>
        <el-button size="small" width="60" type="danger" icon="el-icon-delete" @click="delAll()"
          :disabled="multipleSelection.length === 0">批量删除</el-button>
      </el-form-item>
    </el-form>


    <!--列表
       @selection-change="handleSelectionChange"    //多选，当选择项发生变化时会触发该事件
      @row-click="handleRowClick"    //点击每行时触发事件
      ref="handSelectTest_multipleTable"  //添加属性
      row-key="id" -->
    <el-table size="small" :data="listData" ref="handSelectTest_multipleTable" row-key="id" highlight-current-row
      v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60" />

      <el-table-column label="序号" width="50px" align="center">
        <template slot-scope="scope">
          {{ scope.$index + (formInline.current - 1) * formInline.size + 1 }}
        </template>
      </el-table-column>

      <el-table-column label="评论图片" align="center" width="400px">
        <template slot-scope="scope">
          <span v-for="(imgItem, imgIndex) in scope.row.srcList">
            <el-popover placement="left" trigger="click" width="300">
              <span style="float: right;">
                <i class="el-icon-delete" @click="delImg(scope.$index, scope.row, imgIndex)" />
              </span>

              <img :src="imgItem" width="100%" />
              <img slot="reference" :src="imgItem" :alt="imgItem"
                style="max-height: 100px;max-width: 100px; padding: 5px" />

            </el-popover>
          </span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="evaluationContent" label="评论内容" min-width="160" />
      <el-table-column align="center" prop="path" label="文件夹" width="160" />

      <el-table-column align="center" label="操作" width="260">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.$index, scope.row)" type="primary" size="mini" icon="el-icon-edit">编辑
          </el-button>
          <el-button @click="deleteEvaluation(scope.$index, scope.row)" type="danger" size="mini" icon="el-icon-delete">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item align="center" label="商品名称" prop="deptName">
          <el-input size="small" v-model="editForm.deptName" auto-complete="off" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item align="center" label="商品代码" prop="deptNo">
          <el-input size="small" v-model="editForm.deptNo" auto-complete="off" placeholder="请输入商品代码"></el-input>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { evaluationList, updateEvaluation, deleteEvaluation, getImg, deleteImgByPath } from '../../api/EcommerceManagement'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      multipleSelection:[],
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      imgUrl: '',
      editForm: {
        deptId: '',
        deptName: '',
        deptNo: '',
        token: localStorage.getItem('logintoken')
      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        current: 1,
        size: 10,
        varLable: '',
        varName: '',
        token: localStorage.getItem('logintoken')
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //数据列表
      // 分页参数
      pageparm: {
        current: 1,
        size: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    // 获取公司列表
    getdata(parameter) {
      // 模拟数据开始 evaluationList
      this.loading = true
      // 分页赋值
      this.pageparm.currentPage = this.formInline.current
      this.pageparm.pageSize = this.formInline.size
      // 列表数据
      evaluationList(parameter).then(res => {
        this.loading = false
        if (res.success == false) {
          this.$message({
            type: 'info',
            message: res.msg
          })
        } else {
          for (let item of res.records) {
            item.srcList = [];
            let evaluationImg = item.evaluationImg.split(',')
            for (let itempath of evaluationImg) {
              itempath = itempath.replace(/\s/g,"");
              if (itempath != undefined && itempath != "" && itempath != null) {
                let imgparam = {
                  imgPath: itempath
                }
                getImg(imgparam).then(res => {
                  let blob = new Blob([res.data], { type: 'image/jpeg' });
                  const imageUrl = URL.createObjectURL(blob);
                  item.srcList.push(imageUrl);
                })
              }
            }
          }
          this.listData = res.records
          // 分页赋值
          this.pageparm.currentPage = this.formInline.current
          this.pageparm.pageSize = this.formInline.size
          this.pageparm.total = res.total
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.current = parm.currentPage
      this.formInline.size = parm.pageSize
      this.getdata(this.formInline)
    },

    //点击复选框触发，复选框样式的改变
    handleSelectionChange(val) {
      console.log(val)  //打印选中的行集合
      this.multipleSelection = val;
    },
    //点击行触发，选中或不选中复选框
    handleRowClick(row, column, event) {
      this.$refs.handSelectTest_multipleTable.toggleRowSelection(row);
      console.log(row)  //打印的时当前选中的行
    },

    delImg(index, row, imgIndex) {
      if (index > -1) {
        row.srcList.splice(index, 1);
      }

      let pathArr = row.evaluationImg.split(",");
      let imgPath = pathArr[imgIndex];
      let param = {
        id: row.id,
        imgPath: imgPath
      }

      deleteImgByPath(param)
        .then(res => {
          if (res.success) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('删除失败，请稍后再试！')
        })

    },
    //点击批量删除
    delAll() {
      var arr = []
      //遍历点击选择的对象集合，拿到每一个对象的id添加到新的集合中
      this.multipleSelection.forEach(row => arr.push(row.id))
      this.$confirm('确定删除吗', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'success',
        callback: action => {
          if (action === 'confirm') {
            var res = JSON.stringify(arr);
            this.$notify({
              title: '删除成功',
              message: res,
              type: 'success',
              duration: 2000
            })
            //批量删除
            //delAllIpserver(arr).then(response => {})

            //  this.$notify({
            //                 title: '删除成功',
            //                 message: '',
            //                 type: 'success',
            //                 duration: 2000
            //               })
            //               //this.getList()  //重新加载页面的方法


          }
        }
      })
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.deptId = row.deptId
        this.editForm.deptName = row.deptName
        this.editForm.deptNo = row.deptNo
      } else {
        this.title = '添加'
        this.editForm.deptId = ''
        this.editForm.deptName = ''
        this.editForm.deptNo = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          deptSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '公司保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('公司保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除评论
    deleteEvaluation(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let param = {
            id: row.id
          }
          deleteEvaluation(param)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '删除成功!'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('删除失败，请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>
  
<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>